<template>
	<div class="boxStyle">
		<el-row type="flex" class="row-bg" justify="start" style="margin-bottom: 10px;">
			<el-col :span="1" style="margin-left: 20px;margin-top: 5px; ">名称 :</el-col>
			<el-col :span="3">
				<el-input v-model="name" size="small"></el-input>
			</el-col>
			<el-col :span="1" style="margin-right:50px;margin-left: 50px;">
				<el-button type="warning" size="small" @click="search" icon="el-icon-search">查找</el-button>
			</el-col>
			<el-col :span="1" v-if="addShow!==-1">
				<el-button type="warning" size="small" @click="add" icon="el-icon-plus">新增</el-button>
			</el-col>
		</el-row>
		<el-table ref="multipleTable" stripe border :data="tableLists" tooltip-effect="dark" style="margin-bottom: 10px;width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="name" label="名称" width="300"></el-table-column>
			<el-table-column prop="remark" label="备注" width="300"></el-table-column>
			<el-table-column prop="id" label="页面ID" width="120"></el-table-column>
			<el-table-column label="操作" min-width="100">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" v-if="updateShow!==-1" @click="update(scope.$index,scope.row.id)">编辑</el-button>
					<el-button size="mini" style="margin-top: 5px;margin-left: 0px;" type="danger" v-if="deleShow!==-1" @click="del(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="size" layout="prev, pager, next,total, slot" :total="totals">
			<span style="margin-top: -2px;color: #606266;font-weight: 100;">第<el-input type="text" v-model="inputPage" style="width: 60px;margin: 0 5px;"/>页<el-button size="mini" style="background: #409EFF; color: #fff;margin:0 5px;" @click="handleCurrentChange(inputPage)">跳转</el-button><span>总计{{totalPage}}页</span></span>
		</el-pagination>
		<div class="modal fade" id="info" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:1200px;height: 750px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<ul class="nav nav-tabs info" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">新增/编辑</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:600px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="ruleinfo" :rules="rules" ref="rulecards" label-width="100px" class="demo-ruleForm">
									<input type="hidden" name="id" id="id" v-model="id" />
									<el-col :span="12">
										<el-form-item label="名称:" prop="name">
											<el-input v-model="ruleinfo.name" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="备注:" prop="remark">
											<el-input v-model="ruleinfo.remark" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="24">
										<el-form-item label="内容:">
											<ueditor v-model="form.content" id="infeditor" ref="infeditor" style="width:100%"></ueditor>
										</el-form-item>
									</el-col>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<el-button type="success" size="small" id="save" @click="save('rulecards')" :loading="loading">保存</el-button>
						<el-button type="success" size="small" id="usave" @click="uSave('rulecards')" :loading="loading">保存</el-button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
	</div>
</template>

<script>
	import axios from "axios"
	import ueditor from "components/ueditor.vue"
	import qs from "qs"
	export default {
		data() {
			return {
				name: "",
				addShow: "",
				updateShow: "",
				deleShow: 0,
				tableLists: [],
				currentPage: 1, //当前页
				totals: 0, //分页总条数
				size: 0, //每页条数
				purl: this.$store.state.purl,
				imgurl: this.GLOBAL.imgurl,
				totalPage: 0, //总页数
				inputPage: 1,
				loading:false,
				id: "",
				ruleinfo: {
					name: "",
					remark: ""
				},
				rules: {
					name: [{
						required: true,
						message: "名称不能为空",
						trigger: "change"
					}, {
						max: 50,
						message: "名称不能超过50个字符"
					}]
				},
				form: {
					content: ""
				}
			}
		},
		components: {
			ueditor
		},
		activated() {
			this.name = "";
			this.load(1);
			this.addShow = this.purl[0].findIndex((val) => val === "/interface/information/add") //判断是否拥有增加权限
			this.updateShow = this.purl[0].findIndex((val) => val === "/interface/information/view") //判断是否拥有更新权限\
			this.deleShow = this.purl[0].findIndex((val) => val === "/interface/information/add")
		},
		methods: {
			load(num) {
				axios.get("/interface/information/list?pageNo=" + num).then((res) => {
					this.tableLists = res.data.data.list;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			add() {
				$("#info").modal("show");
//				$(".info li a").text("新增图文");
				$("#save").show();
				$("#usave").hide();
				this.ruleinfo.name = '';
				this.ruleinfo.remark = '';
				this.form.content = '';
			},
			save(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						if(this.form.content == '') {
							this.$message.error("请输入文章内容");
							return;
						}
						this.loading=true;
						var params = {
							name: this.ruleinfo.name,
							remark: this.ruleinfo.remark,
							description: this.form.content
						}
						axios({
							method: 'post',
							url: '/interface/information/add',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading=false;
								$("#info").modal("hide");
								this.load(this.currentPage);
								this.$message.success("新增成功")
							} else {
								this.loading=false;
								var msg = res.data.msg;
								$("#info").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#info").modal("show");
									}
								});
							}
						});
					} else {
						return false;
					}
				})
			},
			update(index, id) {
				$("#info").modal("show");
//				$(".info li a").text("更新图文");
				$("#save").hide();
				$("#usave").show();
				this.id = id;
				axios.get("/interface/information/view?id=" + id).then((res) => {
					this.ruleinfo.name = res.data.data.name;
					this.ruleinfo.remark = res.data.data.remark;
					this.form.content = res.data.data.description;
				})
			},
			uSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						if(this.form.content == '') {
							this.$message.error("请输入文章内容");
							return;
						}
						this.loading=true;
						var params = {
							id: this.id,
							name: this.ruleinfo.name,
							remark: this.ruleinfo.remark,
							description: this.form.content
						}
						axios({
							method: 'post',
							url: '/interface/information/add',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading=false;
								$("#info").modal("hide");
								this.load(this.currentPage);
								this.$message.success("更新成功")
							} else {
								this.loading=false;
								var msg = res.data.msg;
								$("#info").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#info").modal("show");
									}
								});
							}
						});
					} else {
						return false;
					}
				})
			},
			search() {
				axios.post("/interface/information/list?", qs.stringify({
					name: this.name,
					pageNo: 1
				})).then((res) => {
					this.tableLists = res.data.data.list;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			handleCurrentChange(val) {
				this.currentPage = val - 0;
				this.inputPage = val;
				if(this.name = "") {
					axios.get("/interface/information/list?pageNo=" + val).then((res) => {
						this.tableLists = res.data.data.list;
					})
				} else {
					axios.post("/interface/information/list?", qs.stringify({
						name: this.name,
						pageNo: val
					})).then((res) => {
						this.tableLists = res.data.data.list;
						this.totals = res.data.data.pageBean.totalRow; //总条数
						this.size = res.data.data.pageBean.pageSize; //每页条数
						this.totalPage = res.data.data.pageBean.totalPage; //总页数
					})

				}
			},
			del(id) {
				this.$confirm('确认要删除此条数据吗?', '删除警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					axios({
						method: 'post',
						url: '/interface/information/add',
						data: {
							"id": id,
							"deleteFlag": 1
						},
						headers: {
							'Content-Type': 'application/json'
						}
					}).then((res) => {
						this.load(1)
					})
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				}).catch(() => {});
			}
		}
	}
</script>

<style>
	#info .modal-dialog {
		left: 25% !important;
		top: 15% !important;
	}
</style>